/**
 * 
 */
package botactions;

import bot.IRCBot;

/**
 * @author Philip
 *
 */
public class PrivmsgAction implements IRCServerTypeAction {

	@Override
	public void execute(IRCBot bot, String line) {
		int messageIndex = line.substring(1).indexOf(":");
		String[] segments = line.substring(1, messageIndex).split(" ");
		
		String sender = segments[0].substring(0, segments[0].indexOf("!"));
		String senderHostname = segments[0].substring(segments[0].indexOf("@") + 1);
		String receiver = segments[2];
		String message = line.substring(1).substring(messageIndex + 1); 
		
		if (receiver.equals(bot.getNickName()) && !sender.equals(bot.getNickName())) {
			bot.respondToTrigger(sender, sender, senderHostname, message);
		} else {
			bot.respondToTrigger(sender, receiver, senderHostname, message);
		}
	}
}
